import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';
///选择工时类型
class SelectLaborHour extends StatefulWidget {
  @override
  _SelectLaborHourState createState() => _SelectLaborHourState();
}

class _SelectLaborHourState extends State<SelectLaborHour> {

  List<bool> boolList = [];
  List<List<bool>> itemList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
   setState(() {
     for(int i = 0; i < 5; i++ ){
       boolList.add(false);
       itemList.add([]);
       for(int j = 0; j < 5; j++ ){
         itemList[i].add(false);
       }
     }
   });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: customAppBar(context,title: "选择工时类型"),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            height: setWidth(10),
            color: Color(0xffF2F2F2),
          ),
          Container(
            margin: EdgeInsets.only(
                left: setWidth(15), right: setWidth(15), top: setWidth(15)),
            height: setWidth(36),
            decoration: BoxDecoration(
                color: Color(0xffF9F9F9),
                borderRadius: BorderRadius.circular(18)),
            child: TextField(
              maxLines: 1,
              keyboardType: TextInputType.multiline,
              autofocus: false,
              style:
              TextStyle(color: Color(0xff030303), fontSize: setSp(16)),
              cursorColor: YTTheme.theme,
              textAlign: TextAlign.start,
              enableInteractiveSelection: false,
              inputFormatters: [
                WhitelistingTextInputFormatter(
                    RegExp("[a-zA-Z]|[0-9.-_]|[\u4e00-\u9fa5]")),
              ],
              decoration: InputDecoration(
                  contentPadding: EdgeInsets.only(left: setWidth(19)),
                  border: InputBorder.none,
                  focusedBorder: InputBorder.none,
                  enabledBorder: InputBorder.none,
                  prefixIcon: Container(
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {},
                          child: Container(
                              width: setWidth(30),
                              alignment: Alignment.center,
                              child: Column(
                                children: [
                                  Container(
                                    width: setWidth(18),
                                    height: setWidth(19),
                                    child: Image.asset(
                                      "assets/images/search.png",
                                      fit: BoxFit.cover,
                                      color: Color(0xff989898),
                                    ),
                                  ),
                                ],
                              )),
                        )
                      ],
                    ),
                  ),
                  hintText: '输入搜索输搜索工时类型',
                  hintStyle: TextStyle(
                      color: Color(0xff989898), fontSize: setSp(15))),
            ),
          ),
          SizedBox(
            height: setWidth(13),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(
                  bottom: BorderSide(
                    color: Color(0xffF0F0F0),
                    width: 0.5,),
                )
            ),
            padding: EdgeInsets.all(setWidth(15)),
            child: Container(
              child: Text("全部",style: TextStyle(color: Color(0xff321212),fontSize: setSp(16)),),
            ),//
          ),
          Expanded(
              child: Container(
                child: ListView.builder(
                    itemCount: boolList.length,
                    shrinkWrap: true,
                    itemBuilder: (context,index){
                      return GestureDetector(
                        onTap: () {

                        },
                        child: Container(

                          child: Column(
                            children: [
                              Container(
                                decoration: BoxDecoration(
                                    border: Border(
                                      bottom: BorderSide(
                                        color: Color(0xffF0F0F0),
                                        width: 0.5,),
                                    )
                                ),
                                padding: EdgeInsets.all(setWidth(15)),
                                child: Row(
                                  children: [
                                    Container(
                                      child: Text("汽车保养",style: TextStyle(color: Color(0xff323232),fontSize: setSp(15)),),
                                    ),
                                    GestureDetector(
                                      behavior: HitTestBehavior.opaque,
                                      onTap: (){
                                        setState(() {
                                          boolList[index] = !boolList[index];
                                        });
                                      },
                                      child: Row(
                                        mainAxisAlignment: MainAxisAlignment.end,
                                        children: [
                                          Container(
                                            child: Text("${_queryBool(index)}/${itemList[index].length}",style: TextStyle(color: Color(0xff666666),fontSize: setSp(16)),),
                                          ),
                                          SizedBox(width: setWidth(15),
                                            height: setWidth(10),
                                            child: Image.asset(boolList[index]?"assets/images/home_up.png":"assets/images/home_done.png",width: setWidth(15),
                                              height: setWidth(10),fit: BoxFit.cover,),
                                          ),
                                        ],
                                      ),
                                    )
                                  ],
                                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                ),
                              ),
                             Offstage(
                               offstage:boolList[index],
                               child: Container(
                                 padding: EdgeInsets.only(left: setWidth(15),right: setWidth(15)),
                                 child: Column(
                                   mainAxisAlignment: MainAxisAlignment.start,
                                   crossAxisAlignment: CrossAxisAlignment.start,
                                   children: [
                                     GestureDetector(
                                       behavior: HitTestBehavior.opaque,
                                       onTap: (){
                                         setState(() {
                                           for(int i = 0; i < itemList[index].length; i++){
                                             itemList[index][i] = true;
                                           }
                                         });
                                       },

                                       child: Row(
                                         mainAxisAlignment: MainAxisAlignment.start,
                                         children: [
                                           Container(
                                             height: setWidth(19),
                                             width: setWidth(19),
                                             child: Image.asset(_checkBool(index)?"assets/images/labor_check.png":"assets/images/labor_no_check.png",fit: BoxFit.cover,),
                                           ),
                                           SizedBox(
                                             width: setWidth(10),
                                           ),
                                           Container(
                                             child: Text("全选",style: TextStyle(color: Color(0xff333333),fontSize: setSp(16)),),
                                             margin: EdgeInsets.only(top: setWidth(18),bottom: setWidth(18)),
                                           ),
                                         ],
                                       ),
                                     ),

                                     ListView.builder(
                                         shrinkWrap: true,
                                         physics: NeverScrollableScrollPhysics(),
                                         itemCount: 5,
                                         itemBuilder: (context,indexItem){
                                           return GestureDetector(
                                             onTap: (){
                                               setState(() {
                                                 itemList[index][indexItem] = !itemList[index][indexItem];
                                               });
                                             },
                                             child:_checkItem(check: itemList[index][indexItem]),
                                           );
                                         })

                                   ],
                                 ),
                               ),
                             )
                            ],
                          ),
                        ),

                      );
                    }),


              )),
          SizedBox(
            height: setWidth(70),
          ),
        ],

      ),
        //提交
        bottomSheet: confirmButton(title: "确定",
            voidCallback: () {
             Navigator.pop(context);
             })

    );
  }

  //有图item
  Container _checkItem({bool check}) {
    return Container(
      decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              color: Color(0xffF0F0F0),
              width: 0.5,),
          )
      ),
      padding: EdgeInsets.only(top: setWidth(20),bottom: setWidth(20)),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              Container(
                height: setWidth(19),
                width: setWidth(19),
                child: Image.asset(check?"assets/images/labor_check.png":"assets/images/labor_no_check.png",fit: BoxFit.cover,),
              ),
              SizedBox(
                width: setWidth(10),
              ),
              Container(
                child: Text("更换螺丝",style: TextStyle(color: Color(0xff321212),fontSize: setSp(15)),),
              ),//
            ],
          ),
        ],
      ),
    );

  }

   bool _checkBool(int index){
    if(itemList[index].indexOf(false) != -1){
      return false;
    } else{
      return true;
    }
  }
  int _queryBool(int index){
    int count = 0;
    itemList[index].forEach((element) {
      if(element){
        count += 1;
      }

    });
    return count;
}
}
